﻿using System;

namespace Infrastructure.CrossCutting.Converters
{
    public class DataTypeConverter
    {
        public static bool CanConvertTo(Type convertTo, string valueToConvert)
        {
            try
            {
                if (convertTo == typeof(int))
                {
                    int result;
                    if (int.TryParse(valueToConvert, out result)) return true;

                    return false;
                }
                if (convertTo == typeof(string))
                {
                    return true;
                }
                if (convertTo == typeof(double))
                {
                    double d;
                    if (double.TryParse(valueToConvert, out d)) return true;

                    return false;
                }
                if (convertTo == typeof(long))
                {
                    long l;
                    if (long.TryParse(valueToConvert, out l)) return true;

                    return false;
                }
                if (convertTo == typeof(float))
                {
                    float f;
                    if (float.TryParse(valueToConvert, out f)) return true;

                    return false;
                }
                if (convertTo == typeof(bool))
                {
                    bool b;
                    if (bool.TryParse(valueToConvert, out b)) return true;

                    return false;
                }
                if (convertTo == typeof(DateTime))
                {
                    DateTime d;
                    if (DateTime.TryParse(valueToConvert, out d)) return true;

                    return false;
                }
                if (convertTo.BaseType == typeof(Enum))
                {
                    Enum.Parse(convertTo, valueToConvert, true);
                    return true;
                }
            }
            catch (Exception)
            {
                return false;
            }

            //Conversion worked.
            return false;
        }

        public static object ConvertTo(Type type, object val)
        {
            object convertedObject = null;

            if (type == typeof(int))
            {
                convertedObject = Convert.ChangeType(val, typeof(int));
            }
            else if (type == typeof(double))
            {
                convertedObject = Convert.ChangeType(val, typeof(double));
            }
            else if (type == typeof(long))
            {
                convertedObject = Convert.ChangeType(val, typeof(long));
            }
            else if (type == typeof(float))
            {
                convertedObject = Convert.ChangeType(val, typeof(float));
            }
            else if (type == typeof(bool))
            {
                convertedObject = Convert.ChangeType(val, typeof(bool));
            }
            else if (type == typeof(DateTime))
            {
                convertedObject = Convert.ChangeType(val, typeof(DateTime));
            }
            else if (type == typeof(string))
            {
                convertedObject = Convert.ChangeType(val, typeof(string));
            }
            else if (type == typeof(Enum) && val is string)
            {
                convertedObject = Enum.Parse(type, (string)val, true);
            }
            return convertedObject;
        }
    }
}
